package org.ewaf.framework.task;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class EngineAccess implements InitializingBean, ApplicationContextAware {

	private Map<String, Engine> type2Engine = new HashMap<String, Engine>();
	private ApplicationContext applicationContext;

	public Engine getEngine(String type) {
		return type2Engine.get(type);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void afterPropertiesSet() throws Exception {

		Map<String, Engine> name2Engine = applicationContext
				.getBeansOfType(Engine.class);

		for (Map.Entry<String, Engine> entry : name2Engine.entrySet()) {
			if (StringUtils.isEmpty(entry.getValue().getType())) {
				type2Engine.put(entry.getKey(), entry.getValue());
			} else {
				type2Engine.put(entry.getValue().getType(), entry.getValue());
			}
		}

	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;

	}

}
